<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>webdoc</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            webdoc
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li class="active"><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-dva.html">30.cms-10-dva</a></li><li><a href="../html/30.cms-11-front.html">30.cms-11-front</a></li><li><a href="../html/31.cms-12-api.html">31.cms-12-api</a></li><li><a href="../html/31.cms-13-front.html">31.cms-13-front</a></li><li><a href="../html/31.cms-14-deploy.html">31.cms-14-deploy</a></li><li><a href="../html/32.ant.html">32.ant</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. 缓存作用">1. 缓存作用</a></li><li><a href="#t12. 缓存分类">2. 缓存分类</a><ul><li><a href="#t22.1 强制缓存">2.1 强制缓存</a></li><li><a href="#t32.2 对比缓存">2.2 对比缓存</a></li></ul></li><li><a href="#t43. 请求流程">3. 请求流程</a><ul><li><a href="#t53.1 第一次请求">3.1 第一次请求</a></li><li><a href="#t63.2 第二次请求">3.2 第二次请求</a></li></ul></li><li><a href="#t74. 通过最后修改时间来判断缓存是否可用">4. 通过最后修改时间来判断缓存是否可用</a></li><li><a href="#t85. 最后修改时间存在问题">5. 最后修改时间存在问题</a></li><li><a href="#t96. ETag">6. ETag</a></li><li><a href="#t107. 如何干脆不发请求">7. 如何干脆不发请求</a><ul><li><a href="#t117.1 Cache-Control">7.1 Cache-Control</a></li></ul></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. &#x7F13;&#x5B58;&#x4F5C;&#x7528;">1. &#x7F13;&#x5B58;&#x4F5C;&#x7528; <a href="#t01. &#x7F13;&#x5B58;&#x4F5C;&#x7528;"> # </a></h2>
<ul>
<li>&#x51CF;&#x5C11;&#x4E86;&#x5197;&#x4F59;&#x7684;<code>&#x6570;&#x636E;&#x4F20;&#x8F93;</code>&#xFF0C;&#x8282;&#x7701;&#x4E86;&#x7F51;&#x8D39;&#x3002;</li>
<li>&#x51CF;&#x5C11;&#x4E86;&#x670D;&#x52A1;&#x5668;&#x7684;&#x8D1F;&#x62C5;&#xFF0C; &#x5927;&#x5927;&#x63D0;&#x9AD8;&#x4E86;&#x7F51;&#x7AD9;&#x7684;<code>&#x6027;&#x80FD;</code></li>
<li>&#x52A0;&#x5FEB;&#x4E86;&#x5BA2;&#x6237;&#x7AEF;&#x52A0;&#x8F7D;&#x7F51;&#x9875;&#x7684;<code>&#x901F;&#x5EA6;</code></li>
</ul>
<h2 id="t12. &#x7F13;&#x5B58;&#x5206;&#x7C7B;">2. &#x7F13;&#x5B58;&#x5206;&#x7C7B; <a href="#t12. &#x7F13;&#x5B58;&#x5206;&#x7C7B;"> # </a></h2>
<ul>
<li>&#x5F3A;&#x5236;&#x7F13;&#x5B58;&#x5982;&#x679C;&#x751F;&#x6548;&#xFF0C;&#x4E0D;&#x9700;&#x8981;&#x518D;&#x548C;&#x670D;&#x52A1;&#x5668;&#x53D1;&#x751F;&#x4EA4;&#x4E92;&#xFF0C;&#x800C;&#x5BF9;&#x6BD4;&#x7F13;&#x5B58;&#x4E0D;&#x7BA1;&#x662F;&#x5426;&#x751F;&#x6548;&#xFF0C;&#x90FD;&#x9700;&#x8981;&#x4E0E;&#x670D;&#x52A1;&#x7AEF;&#x53D1;&#x751F;&#x4EA4;&#x4E92;</li>
<li>&#x4E24;&#x7C7B;&#x7F13;&#x5B58;&#x89C4;&#x5219;&#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x5B58;&#x5728;&#xFF0C;&#x5F3A;&#x5236;&#x7F13;&#x5B58;&#x4F18;&#x5148;&#x7EA7;&#x9AD8;&#x4E8E;&#x5BF9;&#x6BD4;&#x7F13;&#x5B58;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x5F53;&#x6267;&#x884C;&#x5F3A;&#x5236;&#x7F13;&#x5B58;&#x7684;&#x89C4;&#x5219;&#x65F6;&#xFF0C;&#x5982;&#x679C;&#x7F13;&#x5B58;&#x751F;&#x6548;&#xFF0C;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x7F13;&#x5B58;&#xFF0C;&#x4E0D;&#x518D;&#x6267;&#x884C;&#x5BF9;&#x6BD4;&#x7F13;&#x5B58;&#x89C4;&#x5219;<h3 id="t22.1 &#x5F3A;&#x5236;&#x7F13;&#x5B58;">2.1 &#x5F3A;&#x5236;&#x7F13;&#x5B58; <a href="#t22.1 &#x5F3A;&#x5236;&#x7F13;&#x5B58;"> # </a></h3>
&#x5F3A;&#x5236;&#x7F13;&#x5B58;&#xFF0C;&#x5728;&#x7F13;&#x5B58;&#x6570;&#x636E;&#x672A;&#x5931;&#x6548;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x7F13;&#x5B58;&#x6570;&#x636E;&#xFF0C;&#x90A3;&#x4E48;&#x6D4F;&#x89C8;&#x5668;&#x662F;&#x5982;&#x4F55;&#x5224;&#x65AD;&#x7F13;&#x5B58;&#x6570;&#x636E;&#x662F;&#x5426;&#x5931;&#x6548;&#x5462;&#xFF1F;
&#x6211;&#x4EEC;&#x77E5;&#x9053;&#xFF0C;&#x5728;&#x6CA1;&#x6709;&#x7F13;&#x5B58;&#x6570;&#x636E;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6D4F;&#x89C8;&#x5668;&#x5411;&#x670D;&#x52A1;&#x5668;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x65F6;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x4F1A;&#x5C06;&#x6570;&#x636E;&#x548C;&#x7F13;&#x5B58;&#x89C4;&#x5219;&#x4E00;&#x5E76;&#x8FD4;&#x56DE;&#xFF0C;&#x7F13;&#x5B58;&#x89C4;&#x5219;&#x4FE1;&#x606F;&#x5305;&#x542B;&#x5728;&#x54CD;&#x5E94;header&#x4E2D;&#x3002;</li>
</ul>
<p><img src="http://img.zhufengpeixun.cn/cache2.png" alt=""></p>
<h3 id="t32.2 &#x5BF9;&#x6BD4;&#x7F13;&#x5B58;">2.2 &#x5BF9;&#x6BD4;&#x7F13;&#x5B58; <a href="#t32.2 &#x5BF9;&#x6BD4;&#x7F13;&#x5B58;"> # </a></h3>
<ul>
<li>&#x5BF9;&#x6BD4;&#x7F13;&#x5B58;&#xFF0C;&#x987E;&#x540D;&#x601D;&#x4E49;&#xFF0C;&#x9700;&#x8981;&#x8FDB;&#x884C;&#x6BD4;&#x8F83;&#x5224;&#x65AD;&#x662F;&#x5426;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7F13;&#x5B58;&#x3002;</li>
<li>&#x6D4F;&#x89C8;&#x5668;&#x7B2C;&#x4E00;&#x6B21;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x65F6;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x4F1A;&#x5C06;&#x7F13;&#x5B58;&#x6807;&#x8BC6;&#x4E0E;&#x6570;&#x636E;&#x4E00;&#x8D77;&#x8FD4;&#x56DE;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x5C06;&#x4E8C;&#x8005;&#x5907;&#x4EFD;&#x81F3;&#x7F13;&#x5B58;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x3002;</li>
<li>&#x518D;&#x6B21;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x65F6;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x5C06;&#x5907;&#x4EFD;&#x7684;&#x7F13;&#x5B58;&#x6807;&#x8BC6;&#x53D1;&#x9001;&#x7ED9;&#x670D;&#x52A1;&#x5668;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x6839;&#x636E;&#x7F13;&#x5B58;&#x6807;&#x8BC6;&#x8FDB;&#x884C;&#x5224;&#x65AD;&#xFF0C;&#x5224;&#x65AD;&#x6210;&#x529F;&#x540E;&#xFF0C;&#x8FD4;&#x56DE;304&#x72B6;&#x6001;&#x7801;&#xFF0C;&#x901A;&#x77E5;&#x5BA2;&#x6237;&#x7AEF;&#x6BD4;&#x8F83;&#x6210;&#x529F;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7F13;&#x5B58;&#x6570;&#x636E;&#x3002;</li>
</ul>
<p><img src="http://img.zhufengpeixun.cn/cache4.png" alt=""></p>
<h2 id="t43. &#x8BF7;&#x6C42;&#x6D41;&#x7A0B;">3. &#x8BF7;&#x6C42;&#x6D41;&#x7A0B; <a href="#t43. &#x8BF7;&#x6C42;&#x6D41;&#x7A0B;"> # </a></h2>
<h3 id="t53.1 &#x7B2C;&#x4E00;&#x6B21;&#x8BF7;&#x6C42;">3.1 &#x7B2C;&#x4E00;&#x6B21;&#x8BF7;&#x6C42; <a href="#t53.1 &#x7B2C;&#x4E00;&#x6B21;&#x8BF7;&#x6C42;"> # </a></h3>
<p><img src="http://img.zhufengpeixun.cn/cache6.png" alt=""></p>
<h3 id="t63.2 &#x7B2C;&#x4E8C;&#x6B21;&#x8BF7;&#x6C42;">3.2 &#x7B2C;&#x4E8C;&#x6B21;&#x8BF7;&#x6C42; <a href="#t63.2 &#x7B2C;&#x4E8C;&#x6B21;&#x8BF7;&#x6C42;"> # </a></h3>
<p><img src="http://7xjf2l.com1.z0.glb.clouddn.com/cache.png" alt=""></p>
<h2 id="t74. &#x901A;&#x8FC7;&#x6700;&#x540E;&#x4FEE;&#x6539;&#x65F6;&#x95F4;&#x6765;&#x5224;&#x65AD;&#x7F13;&#x5B58;&#x662F;&#x5426;&#x53EF;&#x7528;">4. &#x901A;&#x8FC7;&#x6700;&#x540E;&#x4FEE;&#x6539;&#x65F6;&#x95F4;&#x6765;&#x5224;&#x65AD;&#x7F13;&#x5B58;&#x662F;&#x5426;&#x53EF;&#x7528; <a href="#t74. &#x901A;&#x8FC7;&#x6700;&#x540E;&#x4FEE;&#x6539;&#x65F6;&#x95F4;&#x6765;&#x5224;&#x65AD;&#x7F13;&#x5B58;&#x662F;&#x5426;&#x53EF;&#x7528;"> # </a></h2>
<ol>
<li>Last-Modified&#xFF1A;&#x54CD;&#x5E94;&#x65F6;&#x544A;&#x8BC9;&#x5BA2;&#x6237;&#x7AEF;&#x6B64;&#x8D44;&#x6E90;&#x7684;<code>&#x6700;&#x540E;&#x4FEE;&#x6539;&#x65F6;&#x95F4;</code></li>
<li><code>If-Modified-Since</code>&#xFF1A;&#x5F53;&#x8D44;&#x6E90;&#x8FC7;&#x671F;&#x65F6;&#xFF08;&#x4F7F;&#x7528;Cache-Control&#x6807;&#x8BC6;&#x7684;max-age&#xFF09;&#xFF0C;&#x53D1;&#x73B0;&#x8D44;&#x6E90;&#x5177;&#x6709;<em>Last-Modified</em>&#x58F0;&#x660E;&#xFF0C;&#x5219;&#x518D;&#x6B21;&#x5411;&#x670D;&#x52A1;&#x5668;&#x8BF7;&#x6C42;&#x65F6;&#x5E26;&#x4E0A;&#x5934;<em>If-Modified-Since</em>&#x3002;</li>
<li>&#x670D;&#x52A1;&#x5668;&#x6536;&#x5230;&#x8BF7;&#x6C42;&#x540E;&#x53D1;&#x73B0;&#x6709;&#x5934;<em>If-Modified-Since</em>&#x5219;&#x4E0E;&#x88AB;&#x8BF7;&#x6C42;&#x8D44;&#x6E90;&#x7684;&#x6700;&#x540E;&#x4FEE;&#x6539;&#x65F6;&#x95F4;&#x8FDB;&#x884C;&#x6BD4;&#x5BF9;&#x3002;&#x82E5;&#x6700;&#x540E;&#x4FEE;&#x6539;&#x65F6;&#x95F4;&#x8F83;&#x65B0;&#xFF0C;&#x8BF4;&#x660E;&#x8D44;&#x6E90;&#x53C8;&#x88AB;&#x6539;&#x52A8;&#x8FC7;&#xFF0C;&#x5219;<code>&#x54CD;&#x5E94;&#x6700;&#x65B0;&#x7684;&#x8D44;&#x6E90;</code>&#x5185;&#x5BB9;&#x5E76;&#x8FD4;&#x56DE;200&#x72B6;&#x6001;&#x7801;&#xFF1B;</li>
<li>&#x82E5;&#x6700;&#x540E;&#x4FEE;&#x6539;&#x65F6;&#x95F4;&#x548C;<em>If-Modified-Since</em>&#x4E00;&#x6837;&#xFF0C;&#x8BF4;&#x660E;&#x8D44;&#x6E90;&#x6CA1;&#x6709;&#x4FEE;&#x6539;&#xFF0C;&#x5219;&#x54CD;&#x5E94;304&#x8868;&#x793A;<code>&#x672A;&#x66F4;&#x65B0;</code>&#xFF0C;&#x544A;&#x77E5;&#x6D4F;&#x89C8;&#x5668;&#x7EE7;&#x7EED;&#x4F7F;&#x7528;&#x6240;&#x4FDD;&#x5B58;&#x7684;&#x7F13;&#x5B58;&#x6587;&#x4EF6;&#x3002;</li>
</ol>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">let</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> mime = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;mime&apos;</span>);
http.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
    <span class="hljs-keyword">let</span> file = path.join(__dirname, req.url);
    fs.stat(file, (err, stat) =&gt; {
        <span class="hljs-keyword">if</span> (err) {
            sendError(err, req, res, file, stat);
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">let</span> ifModifiedSince = req.headers[<span class="hljs-string">&apos;if-modified-since&apos;</span>];
            <span class="hljs-keyword">if</span> (ifModifiedSince) {
                <span class="hljs-keyword">if</span> (ifModifiedSince == stat.ctime.toGMTString()) {
                    res.writeHead(<span class="hljs-number">304</span>);
                    res.end();
                } <span class="hljs-keyword">else</span> {
                    send(req, res, file, stat);
                }
            } <span class="hljs-keyword">else</span> {
                send(req, res, file, stat);
            }
        }
    });
}).listen(<span class="hljs-number">8080</span>);
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">send</span>(<span class="hljs-params">req, res, file, stat</span>) </span>{
    res.setHeader(<span class="hljs-string">&apos;Last-Modified&apos;</span>, stat.ctime.toGMTString());
    res.writeHead(<span class="hljs-number">200</span>, { <span class="hljs-string">&apos;Content-Type&apos;</span>: mime.getType(file) });
    fs.createReadStream(file).pipe(res);
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sendError</span>(<span class="hljs-params">err, req, res, file, stat</span>) </span>{
    res.writeHead(<span class="hljs-number">400</span>, { <span class="hljs-string">&quot;Content-Type&quot;</span>: <span class="hljs-string">&apos;text/html&apos;</span> });
    res.end(err ? err.toString() : <span class="hljs-string">&quot;Not Found&quot;</span>);
}
</code></pre>
<h2 id="t85. &#x6700;&#x540E;&#x4FEE;&#x6539;&#x65F6;&#x95F4;&#x5B58;&#x5728;&#x95EE;&#x9898;">5. &#x6700;&#x540E;&#x4FEE;&#x6539;&#x65F6;&#x95F4;&#x5B58;&#x5728;&#x95EE;&#x9898; <a href="#t85. &#x6700;&#x540E;&#x4FEE;&#x6539;&#x65F6;&#x95F4;&#x5B58;&#x5728;&#x95EE;&#x9898;"> # </a></h2>
<ol>
<li>&#x67D0;&#x4E9B;&#x670D;&#x52A1;&#x5668;&#x4E0D;&#x80FD;&#x7CBE;&#x786E;&#x5F97;&#x5230;&#x6587;&#x4EF6;&#x7684;<code>&#x6700;&#x540E;&#x4FEE;&#x6539;&#x65F6;&#x95F4;</code>&#xFF0C; &#x8FD9;&#x6837;&#x5C31;&#x65E0;&#x6CD5;&#x901A;&#x8FC7;&#x6700;&#x540E;&#x4FEE;&#x6539;&#x65F6;&#x95F4;&#x6765;&#x5224;&#x65AD;&#x6587;&#x4EF6;&#x662F;&#x5426;&#x66F4;&#x65B0;&#x4E86;&#x3002;</li>
<li>&#x67D0;&#x4E9B;&#x6587;&#x4EF6;&#x7684;&#x4FEE;&#x6539;&#x975E;&#x5E38;&#x9891;&#x7E41;&#xFF0C;&#x5728;&#x79D2;&#x4EE5;&#x4E0B;&#x7684;&#x65F6;&#x95F4;&#x5185;&#x8FDB;&#x884C;&#x4FEE;&#x6539;. Last-Modified&#x53EA;&#x80FD;<code>&#x7CBE;&#x786E;&#x5230;&#x79D2;</code>&#x3002;</li>
<li>&#x4E00;&#x4E9B;&#x6587;&#x4EF6;&#x7684;&#x6700;&#x540E;&#x4FEE;&#x6539;&#x65F6;&#x95F4;&#x6539;&#x53D8;&#x4E86;&#xFF0C;&#x4F46;&#x662F;<code>&#x5185;&#x5BB9;&#x5E76;&#x672A;&#x6539;&#x53D8;</code>&#x3002; &#x6211;&#x4EEC;&#x4E0D;&#x5E0C;&#x671B;&#x5BA2;&#x6237;&#x7AEF;&#x8BA4;&#x4E3A;&#x8FD9;&#x4E2A;&#x6587;&#x4EF6;&#x4FEE;&#x6539;&#x4E86;&#x3002;</li>
<li>&#x5982;&#x679C;&#x540C;&#x6837;&#x7684;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x4F4D;&#x4E8E;&#x591A;&#x4E2A;<code>CDN</code>&#x670D;&#x52A1;&#x5668;&#x4E0A;&#x7684;&#x65F6;&#x5019;&#x5185;&#x5BB9;&#x867D;&#x7136;&#x4E00;&#x6837;&#xFF0C;&#x4FEE;&#x6539;&#x65F6;&#x95F4;&#x4E0D;&#x4E00;&#x6837;&#x3002;</li>
</ol>
<h2 id="t96. ETag">6. ETag <a href="#t96. ETag"> # </a></h2>
<p>ETag&#x662F;&#x5B9E;&#x4F53;&#x6807;&#x7B7E;&#x7684;&#x7F29;&#x5199;&#xFF0C;&#x6839;&#x636E;&#x5B9E;&#x4F53;&#x5185;&#x5BB9;&#x751F;&#x6210;&#x7684;&#x4E00;&#x6BB5;<code>hash</code>&#x5B57;&#x7B26;&#x4E32;,&#x53EF;&#x4EE5;&#x6807;&#x8BC6;&#x8D44;&#x6E90;&#x7684;&#x72B6;&#x6001;&#x3002;&#x5F53;&#x8D44;&#x6E90;&#x53D1;&#x751F;&#x6539;&#x53D8;&#x65F6;&#xFF0C;ETag&#x4E5F;&#x968F;&#x4E4B;&#x53D1;&#x751F;&#x53D8;&#x5316;&#x3002;
ETag&#x662F;Web&#x670D;&#x52A1;&#x7AEF;&#x4EA7;&#x751F;&#x7684;&#xFF0C;&#x7136;&#x540E;&#x53D1;&#x7ED9;&#x6D4F;&#x89C8;&#x5668;&#x5BA2;&#x6237;&#x7AEF;&#x3002;</p>
<ol>
<li>&#x5BA2;&#x6237;&#x7AEF;&#x60F3;&#x5224;&#x65AD;&#x7F13;&#x5B58;&#x662F;&#x5426;&#x53EF;&#x7528;&#x53EF;&#x4EE5;&#x5148;&#x83B7;&#x53D6;&#x7F13;&#x5B58;&#x4E2D;&#x6587;&#x6863;&#x7684;<em>ETag</em>&#xFF0C;&#x7136;&#x540E;&#x901A;&#x8FC7;<em>If-None-Match</em>&#x53D1;&#x9001;&#x8BF7;&#x6C42;&#x7ED9;Web&#x670D;&#x52A1;&#x5668;&#x8BE2;&#x95EE;&#x6B64;&#x7F13;&#x5B58;&#x662F;&#x5426;&#x53EF;&#x7528;&#x3002;</li>
<li>&#x670D;&#x52A1;&#x5668;&#x6536;&#x5230;&#x8BF7;&#x6C42;&#xFF0C;&#x5C06;&#x670D;&#x52A1;&#x5668;&#x7684;&#x4E2D;&#x6B64;&#x6587;&#x4EF6;&#x7684;<em>ETag</em>,&#x8DDF;&#x8BF7;&#x6C42;&#x5934;&#x4E2D;&#x7684;<em>If-None-Match</em>&#x76F8;&#x6BD4;&#x8F83;,&#x5982;&#x679C;&#x503C;&#x662F;&#x4E00;&#x6837;&#x7684;,&#x8BF4;&#x660E;&#x7F13;&#x5B58;&#x8FD8;&#x662F;&#x6700;&#x65B0;&#x7684;,Web&#x670D;&#x52A1;&#x5668;&#x5C06;&#x53D1;&#x9001;<em>304 Not Modified</em>&#x54CD;&#x5E94;&#x7801;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#x8868;&#x793A;&#x7F13;&#x5B58;&#x672A;&#x4FEE;&#x6539;&#x8FC7;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x3002;</li>
<li>&#x5982;&#x679C;&#x4E0D;&#x4E00;&#x6837;&#x5219;Web&#x670D;&#x52A1;&#x5668;&#x5C06;&#x53D1;&#x9001;&#x8BE5;&#x6587;&#x6863;&#x7684;&#x6700;&#x65B0;&#x7248;&#x672C;&#x7ED9;&#x6D4F;&#x89C8;&#x5668;&#x5BA2;&#x6237;&#x7AEF;</li>
</ol>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">let</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> mime = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;mime&apos;</span>);
<span class="hljs-keyword">let</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;crypto&apos;</span>);
http.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
    <span class="hljs-keyword">let</span> file = path.join(__dirname, req.url);
    fs.stat(file, (err, stat) =&gt; {
        <span class="hljs-keyword">if</span> (err) {
            sendError(err, req, res, file, stat);
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">let</span> ifNoneMatch = req.headers[<span class="hljs-string">&apos;if-none-match&apos;</span>];
            <span class="hljs-keyword">let</span> etag = crypto.createHash(<span class="hljs-string">&apos;sha1&apos;</span>).update(stat.ctime.toGMTString() + stat.size).digest(<span class="hljs-string">&apos;hex&apos;</span>);
            <span class="hljs-keyword">if</span> (ifNoneMatch) {
                <span class="hljs-keyword">if</span> (ifNoneMatch == etag) {
                    res.writeHead(<span class="hljs-number">304</span>);
                    res.end();
                } <span class="hljs-keyword">else</span> {
                    send(req, res, file, etag);
                }
            } <span class="hljs-keyword">else</span> {
                send(req, res, file, etag);
            }
        }
    });
}).listen(<span class="hljs-number">8080</span>);
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">send</span>(<span class="hljs-params">req, res, file, etag</span>) </span>{
    res.setHeader(<span class="hljs-string">&apos;ETag&apos;</span>, etag);
    res.writeHead(<span class="hljs-number">200</span>, { <span class="hljs-string">&apos;Content-Type&apos;</span>: mime.lookup(file) });
    fs.createReadStream(file).pipe(res);
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sendError</span>(<span class="hljs-params">err, req, res, file, etag</span>) </span>{
    res.writeHead(<span class="hljs-number">400</span>, { <span class="hljs-string">&quot;Content-Type&quot;</span>: <span class="hljs-string">&apos;text/html&apos;</span> });
    res.end(err ? err.toString() : <span class="hljs-string">&quot;Not Found&quot;</span>);
}
</code></pre>
<h2 id="t107. &#x5982;&#x4F55;&#x5E72;&#x8106;&#x4E0D;&#x53D1;&#x8BF7;&#x6C42;">7. &#x5982;&#x4F55;&#x5E72;&#x8106;&#x4E0D;&#x53D1;&#x8BF7;&#x6C42; <a href="#t107. &#x5982;&#x4F55;&#x5E72;&#x8106;&#x4E0D;&#x53D1;&#x8BF7;&#x6C42;"> # </a></h2>
<ul>
<li>&#x6D4F;&#x89C8;&#x5668;&#x4F1A;&#x5C06;&#x6587;&#x4EF6;&#x7F13;&#x5B58;&#x5230;Cache&#x76EE;&#x5F55;&#xFF0C;&#x7B2C;&#x4E8C;&#x6B21;&#x8BF7;&#x6C42;&#x65F6;&#x6D4F;&#x89C8;&#x5668;&#x4F1A;&#x5148;&#x68C0;&#x67E5;Cache&#x76EE;&#x5F55;&#x4E0B;&#x662F;&#x5426;&#x542B;&#x6709;&#x8BE5;&#x6587;&#x4EF6;&#xFF0C;&#x5982;&#x679C;&#x6709;&#xFF0C;&#x5E76;&#x4E14;&#x8FD8;&#x6CA1;&#x5230;<strong>Expires</strong>&#x8BBE;&#x7F6E;&#x7684;&#x65F6;&#x95F4;&#xFF0C;&#x5373;&#x6587;&#x4EF6;&#x8FD8;&#x6CA1;&#x6709;&#x8FC7;&#x671F;&#xFF0C;&#x90A3;&#x4E48;&#x6B64;&#x65F6;&#x6D4F;&#x89C8;&#x5668;&#x5C06;&#x76F4;&#x63A5;&#x4ECE;<strong>Cache</strong>&#x76EE;&#x5F55;&#x4E2D;&#x8BFB;&#x53D6;&#x6587;&#x4EF6;&#xFF0C;&#x800C;&#x4E0D;&#x518D;&#x53D1;&#x9001;&#x8BF7;&#x6C42;</li>
<li><strong>Expires</strong>&#x662F;&#x670D;&#x52A1;&#x5668;&#x54CD;&#x5E94;&#x6D88;&#x606F;&#x5934;&#x5B57;&#x6BB5;&#xFF0C;&#x5728;&#x54CD;&#x5E94;<em>http</em>&#x8BF7;&#x6C42;&#x65F6;&#x544A;&#x8BC9;&#x6D4F;&#x89C8;&#x5668;&#x5728;&#x8FC7;&#x671F;&#x65F6;&#x95F4;&#x524D;&#x6D4F;&#x89C8;&#x5668;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4ECE;&#x6D4F;&#x89C8;&#x5668;&#x7F13;&#x5B58;&#x53D6;&#x6570;&#x636E;&#xFF0C;&#x800C;&#x65E0;&#x9700;&#x518D;&#x6B21;&#x8BF7;&#x6C42;,&#x8FD9;&#x662F;HTTP1.0&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x73B0;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x5747;&#x9ED8;&#x8BA4;&#x4F7F;&#x7528;HTTP1.1,&#x6240;&#x4EE5;&#x57FA;&#x672C;&#x53EF;&#x4EE5;&#x5FFD;&#x7565;</li>
<li><strong>Cache-Control</strong>&#x4E0E;<strong>Expires</strong>&#x7684;&#x4F5C;&#x7528;&#x4E00;&#x81F4;&#xFF0C;&#x90FD;&#x662F;&#x6307;&#x660E;&#x5F53;&#x524D;&#x8D44;&#x6E90;&#x7684;&#x6709;&#x6548;&#x671F;&#xFF0C;&#x63A7;&#x5236;&#x6D4F;&#x89C8;&#x5668;&#x662F;&#x5426;&#x76F4;&#x63A5;&#x4ECE;&#x6D4F;&#x89C8;&#x5668;&#x7F13;&#x5B58;&#x53D6;&#x6570;&#x636E;&#x8FD8;&#x662F;&#x91CD;&#x65B0;&#x53D1;&#x8BF7;&#x6C42;&#x5230;&#x670D;&#x52A1;&#x5668;&#x53D6;&#x6570;&#x636E;,&#x5982;&#x679C;&#x540C;&#x65F6;&#x8BBE;&#x7F6E;&#x7684;&#x8BDD;&#xFF0C;&#x5176;&#x4F18;&#x5148;&#x7EA7;&#x9AD8;&#x4E8E;<strong>Expires</strong></li>
</ul>
<h3 id="t117.1 Cache-Control">7.1 Cache-Control <a href="#t117.1 Cache-Control"> # </a></h3>
<ul>
<li>private &#x5BA2;&#x6237;&#x7AEF;&#x53EF;&#x4EE5;&#x7F13;&#x5B58;</li>
<li>public &#x5BA2;&#x6237;&#x7AEF;&#x548C;&#x4EE3;&#x7406;&#x670D;&#x52A1;&#x5668;&#x90FD;&#x53EF;&#x4EE5;&#x7F13;&#x5B58;</li>
<li>max-age=60 &#x7F13;&#x5B58;&#x5185;&#x5BB9;&#x5C06;&#x5728;60&#x79D2;&#x540E;&#x5931;&#x6548;</li>
<li>no-cache &#x9700;&#x8981;&#x4F7F;&#x7528;&#x5BF9;&#x6BD4;&#x7F13;&#x5B58;&#x9A8C;&#x8BC1;&#x6570;&#x636E;,&#x5F3A;&#x5236;&#x5411;&#x6E90;&#x670D;&#x52A1;&#x5668;&#x518D;&#x6B21;&#x9A8C;&#x8BC1;</li>
<li>no-store &#x6240;&#x6709;&#x5185;&#x5BB9;&#x90FD;&#x4E0D;&#x4F1A;&#x7F13;&#x5B58;&#xFF0C;&#x5F3A;&#x5236;&#x7F13;&#x5B58;&#x548C;&#x5BF9;&#x6BD4;&#x7F13;&#x5B58;&#x90FD;&#x4E0D;&#x4F1A;&#x89E6;&#x53D1;</li>
</ul>
<blockquote>
<p>Cache-Control:private, max-age=60, no-cache</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">let</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> mime = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;mime&apos;</span>);
<span class="hljs-keyword">let</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;crypto&apos;</span>);
http.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
    <span class="hljs-keyword">let</span> file = path.join(__dirname, req.url);
    <span class="hljs-built_in">console</span>.log(file);

    fs.stat(file, (err, stat) =&gt; {
        <span class="hljs-keyword">if</span> (err) {
            sendError(err, req, res, file, stat);
        } <span class="hljs-keyword">else</span> {
            send(req, res, file);
        }
    });
}).listen(<span class="hljs-number">8080</span>);
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">send</span>(<span class="hljs-params">req, res, file</span>) </span>{
    <span class="hljs-keyword">let</span> expires = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>(<span class="hljs-built_in">Date</span>.now() + <span class="hljs-number">60</span> * <span class="hljs-number">1000</span>);
    res.setHeader(<span class="hljs-string">&apos;Expires&apos;</span>, expires.toUTCString());
    res.setHeader(<span class="hljs-string">&apos;Cache-Control&apos;</span>, <span class="hljs-string">&apos;max-age=60&apos;</span>);
    res.writeHead(<span class="hljs-number">200</span>, { <span class="hljs-string">&apos;Content-Type&apos;</span>: mime.lookup(file) });
    fs.createReadStream(file).pipe(res);
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sendError</span>(<span class="hljs-params">err, req, res, file, etag</span>) </span>{
    res.writeHead(<span class="hljs-number">400</span>, { <span class="hljs-string">&quot;Content-Type&quot;</span>: <span class="hljs-string">&apos;text/html&apos;</span> });
    res.end(err ? err.toString() : <span class="hljs-string">&quot;Not Found&quot;</span>);
}
</code></pre>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
